<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.company.ems.model.User" %>
<%@ page import="java.util.List" %>
<%@ page import="com.company.ems.model.Employee" %>
<%
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect("./login.jsp");
        return;
    }
    
    List<Employee> employees = (List<Employee>) request.getAttribute("employees");
    Integer totalEmployees = (Integer) request.getAttribute("totalEmployees");
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>员工管理系统 - 仪表板</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <div class="container">
        <!-- 头部 -->
        <div class="header">
            <h1>员工管理系统</h1>
            <div class="user-info">
                <span>欢迎，<%= user.getUsername() %></span>
                <a href="./logout" class="btn btn-secondary">退出登录</a>
            </div>
        </div>
        
        <!-- 导航栏 -->
        <div class="nav">
            <a href="./dashboard" class="active">仪表板</a>
            <a href="./employee/">员工管理</a>
            <a href="./employee/add">添加员工</a>
        </div>
        
        <!-- 内容区域 -->
        <div class="content">
            <div class="stats-card fade-in">
                <h3><%= totalEmployees != null ? totalEmployees : 0 %></h3>
                <p>员工总数</p>
            </div>
            
            <div class="card fade-in">
                <div class="card-header">
                    最近添加的员工
                </div>
                <div class="card-body">
                    <% if (employees != null && !employees.isEmpty()) { %>
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>姓名</th>
                                    <th>部门</th>
                                    <th>职位</th>
                                    <th>薪资</th>
                                    <th>入职时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <% 
                                int count = 0;
                                for (Employee emp : employees) { 
                                    if (count >= 5) break; // 只显示前5条
                                    count++;
                                %>
                                    <tr>
                                        <td><%= emp.getName() %></td>
                                        <td><%= emp.getDepartment() %></td>
                                        <td><%= emp.getPosition() %></td>
                                        <td>¥<%= String.format("%.2f", emp.getSalary()) %></td>
                                        <td><%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(emp.getHireDate()) %></td>
                                        <td>
                                            <a href="./employee/view?id=<%= emp.getId() %>" class="btn btn-primary">查看</a>
                                            <a href="./employee/edit?id=<%= emp.getId() %>" class="btn btn-warning">编辑</a>
                                        </td>
                                    </tr>
                                <% } %>
                            </tbody>
                        </table>
                        
                        <% if (employees.size() > 5) { %>
                            <div style="text-align: center; margin-top: 20px;">
                                <a href="./employee/" class="btn btn-primary">查看全部员工</a>
                            </div>
                        <% } %>
                    <% } else { %>
                        <p style="text-align: center; color: #6c757d; padding: 20px;">
                            暂无员工数据，<a href="./employee/add" style="color: #007bff;">点击这里添加第一个员工</a>
                        </p>
                    <% } %>
                </div>
            </div>
            
            <div class="card fade-in">
                <div class="card-header">
                    快速操作
                </div>
                <div class="card-body">
                    <div style="display: flex; gap: 15px; flex-wrap: wrap;">
                        <a href="./employee/add" class="btn btn-success">添加新员工</a>
                        <a href="./employee/" class="btn btn-primary">查看所有员工</a>
                        <a href="./employee/search" class="btn btn-warning">搜索员工</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>